//
//  KY1010AdvertView.m
//  KYMobClick
//
//  Created by 01 on 16/11/8.
//  Copyright © 2016年 Czj. All rights reserved.
//

#import "KY1010AdvertView.h"

@interface KY1010AdvertView ()
@property (nonatomic, strong) UIImageView   *imgView1;
@property (nonatomic, strong) UIImageView   *imgView2;
@property (nonatomic, strong) UILabel       *label1;
@property (nonatomic, strong) UILabel       *label2;
@property (nonatomic, strong) NSMutableDictionary      *dict;
@end

@implementation KY1010AdvertView

#pragma mark - Init
- (instancetype)init {
    self = [super init];
    if (self) {
        _imgView1 = [[UIImageView alloc] init];
        _imgView2 = [[UIImageView alloc] init];
        _label1 = [[UILabel alloc] init];
        _label1.textAlignment = NSTextAlignmentCenter;
        _label2 = [[UILabel alloc] init];
        _label2.textAlignment = NSTextAlignmentCenter;
        
        [self addSubview:_imgView1];
        [self addSubview:_imgView2];
        [self addSubview:_label1];
        [self addSubview:_label2];
    }
    return self;
}

#pragma mark - Public methods
/**
 接收数据填充视图
 */
- (void) createContent:(NSDictionary *)dataDic blockHeight:(void (^)(NSInteger))viewHeight {
    self.dict = [NSMutableDictionary dictionaryWithDictionary:dataDic];
    NSString *pathDocument = [KYSystemInfo getSaveAdvertImagePath];
    NSString *imgUrl;
    NSString *leftText;
    NSString *imgUrl1;
    NSString *rightText;
    NSString *leftImagePath;
    NSString *rightImagePath;
    if (dataDic && dataDic[@"data"]) {
        imgUrl = dataDic[@"data"][@"imageurl"];
        leftText = dataDic[@"data"][@"headline"];
        leftImagePath = [pathDocument stringByAppendingString:[NSString stringWithFormat:@"left%@.png",self.dict[@"data"][@"adsid"]]];
    }
    if (dataDic && dataDic[@"data1"]) {
        imgUrl1 = dataDic[@"data1"][@"imageurl"];
        rightText = dataDic[@"data1"][@"headline"];
        rightImagePath = [pathDocument stringByAppendingString:[NSString stringWithFormat:@"right%@.png",self.dict[@"data"][@"adsid"]]];
    }
    
    CGFloat img1X = KY_MARGIN;
    CGFloat imgY = KY_MARGIN;
    CGFloat imgW = (kDeviceWidth - KY_MARGIN * 2.5 ) / 2;
    CGFloat imgH = kCurrentScreenWidth(99.6);
    
    _imgView1.frame = CGRectMake(img1X, imgY, imgW, imgH);
    UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(img1X, imgY, imgW, imgH)];
    btn1.tag = 1;
    [btn1 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn1];
    UIImage *leftCacheImage = [UIImage imageWithContentsOfFile:leftImagePath];
    [_imgView1 KYSD_setImageWithPreviousCachedImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:leftCacheImage options:KYSDWebImageAllowInvalidSSLCertificates progress:nil completed:^(UIImage *image, NSError *error, KYSDImageCacheType cacheType, NSURL *imageURL) {
        if (image) {
            [UIImagePNGRepresentation(image) writeToFile:leftImagePath atomically:YES];
            CGSize size = image.size;
            CGFloat height= size.height;
            CGFloat width = size.width;
//            KYLog(@"上图下文广告1高度：%f；宽度：%f",height,width);
            CGRect frame = _imgView1.frame;
            frame.size.height = frame.size.width * height / width;
            _imgView1.frame = frame;
            btn1.frame = _imgView1.frame;
            
            CGFloat label1Y = CGRectGetMaxY(_imgView1.frame) + KY_MARGIN / 2;
            _label1.frame = CGRectMake(img1X, label1Y, imgW, 0);
            _label1.font = kSystemFont(kDefaultfontsize);
            _label1.numberOfLines = 0;
            _label1.text = leftText;
            [_label1 sizeToFit];
            _label1.centerX = _imgView1.centerX;
            [self postPVData];
        }else{
//            KYLog(@"上图下文广告1加载失败");
        }
    }];
    
    CGFloat img2X = CGRectGetMaxX(_imgView1.frame) + KY_MARGIN / 2;
    _imgView2.frame = CGRectMake(img2X, imgY, imgW, imgH);
    UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(img2X, imgY, imgW, imgH)];
    btn2.tag = 2;
    [btn2 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn2];
    UIImage *rightCacheImage = [UIImage imageWithContentsOfFile:rightImagePath];
    [_imgView2 KYSD_setImageWithPreviousCachedImageWithURL:[NSURL URLWithString:imgUrl1] placeholderImage:rightCacheImage options:KYSDWebImageAllowInvalidSSLCertificates progress:nil completed:^(UIImage *image, NSError *error, KYSDImageCacheType cacheType, NSURL *imageURL) {
        if (image) {
            [UIImagePNGRepresentation(image) writeToFile:rightImagePath atomically:YES];
            CGSize size = image.size;
            CGFloat height= size.height;
            CGFloat width = size.width;
//            KYLog(@"上图下文广告2高度：%f；宽度：%f",height,width);
            CGRect frame = _imgView2.frame;
            frame.size.height = frame.size.width * height / width;
            _imgView2.frame = frame;
            btn2.frame = _imgView2.frame;
            _label2.numberOfLines = 0;
            _label2.font = kSystemFont(kDefaultfontsize);
            _label2.text = rightText;
            _label2.frame = CGRectMake(img2X, _label1.frame.origin.y, imgW,0);
            [_label2 sizeToFit];
            _label2.centerX = _imgView2.centerX;
            _viewHeight = (CGRectGetMaxY(_label1.frame) >= CGRectGetMaxY(_label2.frame) ? CGRectGetMaxY(_label1.frame) : CGRectGetMaxY(_label2.frame)) + KY_MARGIN;
            viewHeight(_viewHeight);
        }else{
//            KYLog(@"上图下文广告2加载失败");
        }
    }];
}

/**
 统计PV数据
 */
- (void)postPVData {
    //发送统计数据
    [[NSNotificationCenter defaultCenter] postNotificationName:@"advertPVData" object:self.dict[@"data"]];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"advertPVData" object:self.dict[@"data1"]];
}

#pragma mark - Response methods
- (void)buttonClick:(UIButton *)btn {
    if (btn.tag == 1) {
        if (self.dict) {
//            NSMutableDictionary *dataDict = [NSMutableDictionary dictionaryWithDictionary:self.dict];
//            [dataDict setObject:kOnClick forKey:@"clickStatus"];
            if (self.delegate && [self.delegate respondsToSelector:@selector(clickView:)]) {
                [self.delegate clickView:self.dict[@"data"]];
            }
        }
    }
    if (btn.tag == 2) {
        if (self.dict && self.dict[@"data1"]) {
//            NSMutableDictionary *dataDict = [NSMutableDictionary dictionaryWithDictionary:self.dict];
//            [dataDict setObject:self.dict[@"data1"] forKey:@"data"];
//            [dataDict setObject:kOnClick forKey:@"clickStatus"];
            if (self.delegate && [self.delegate respondsToSelector:@selector(clickView:)]) {
                [self.delegate clickView:self.dict[@"data1"]];
            }
        }
    }
}

#pragma mark - Lazy load
- (NSMutableDictionary *)dict {
    if (!_dict) {
        _dict = [[NSMutableDictionary alloc] init];
    }
    return _dict;
}

@end
